//
//  ConnectionHTTP.m
//  Login
//
//  Created by FER on 22/10/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ConnectionHTTP.h"


@implementation ConnectionHTTP


+ (NSString*) doRequest: (NSString*)urlString {
	//prepar request
	NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    
    //Set the host address
    NSString *host = @"http://192.168.1.105/";
    NSString *concatenado = [host stringByAppendingString:urlString];
    
	[request setURL:[NSURL URLWithString:concatenado]];
	[request setHTTPMethod:@"POST"];
	
	//set headers
	NSString *contentType = [NSString stringWithFormat:@"text/xml"];
	[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
	
	//create the body
	NSMutableData *postBody = [NSMutableData data];
	[postBody appendData:[[NSString stringWithFormat:@"<xml>"] dataUsingEncoding:NSUTF8StringEncoding]];
	[postBody appendData:[[NSString stringWithFormat:@"<yourcode/>"] dataUsingEncoding:NSUTF8StringEncoding]];
	[postBody appendData:[[NSString stringWithFormat:@"</xml>"] dataUsingEncoding:NSUTF8StringEncoding]];
	
	//post
	[request setHTTPBody:postBody];
	
	//get response
	NSHTTPURLResponse* urlResponse = nil;  
	NSError *error = [[NSError alloc] init];  
	NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];  
	NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
	//NSLog(@"Response Code: %d", [urlResponse statusCode]);
	if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
		//here you get the response
		return result;
	}
	return nil;
}



+ (NSString*) doRequestBody: (NSString*)StringURL: (NSString*)Body
{
	//prepar request
	NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    
    //Set the host address
    NSString *host = @"http://192.168.1.105/";
    NSString *concatenado = [host stringByAppendingString:StringURL];
    
	[request setURL:[NSURL URLWithString:concatenado]];
	[request setHTTPMethod:@"POST"];
	
	//set headers
	NSString *contentType = [NSString stringWithFormat:@"text/xml"];
	[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
	
	//create the body
    NSMutableData *postBody = [NSMutableData data];
	[postBody appendData:[[NSString stringWithFormat:@"%@",Body] dataUsingEncoding:NSUTF8StringEncoding]];
	
	//post
	[request setHTTPBody:postBody];
	
	//get response
	NSHTTPURLResponse* urlResponse = nil;  
	NSError *error = [[NSError alloc] init];  
	NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];  
	NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
	//NSLog(@"Response Code: %d", [urlResponse statusCode]);
	if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
		//here you get the response
		return result;
	}
	return nil;
}


+ (NSString*) doRequestBodyData: (NSString*)StringURL: (NSData*)bodyData
{
	//prepar request
	NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    
    //Set the host address
    NSString *host = @"http://192.168.1.105/";
    NSString *concatenado = [host stringByAppendingString:StringURL];
    
	[request setURL:[NSURL URLWithString:concatenado]];
	[request setHTTPMethod:@"POST"];
	
	//set headers
	NSString *contentType = [NSString stringWithFormat:@"text/xml"];
	[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
	
	//create the body
	NSMutableData *postBody = [NSMutableData data];
	[postBody appendData:[NSData dataWithData:bodyData]];
	
    //post
	[request setHTTPBody:postBody];
	
	//get response
	NSHTTPURLResponse* urlResponse = nil;  
	NSError *error = [[NSError alloc] init];  
	NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];  
	NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
	//NSLog(@"Response Code: %d", [urlResponse statusCode]);
	if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
		//here you get the response
		return result;
	}
	return nil;
}

+ (NSData*)   doRequestgetData: (NSString*)StringURL;
{
    //prepar request
	NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    
    //Set the host address
    NSString *host = @"http://192.168.1.105/";
    NSString *concatenado = [host stringByAppendingString:StringURL];
    
	[request setURL:[NSURL URLWithString:concatenado]];
	[request setHTTPMethod:@"POST"];
	
	//set headers
	NSString *contentType = [NSString stringWithFormat:@"text/xml"];
	[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
	
	//create the body
	NSMutableData *postBody = [NSMutableData data];
	[postBody appendData:[[NSString stringWithFormat:@"<xml>"] dataUsingEncoding:NSUTF8StringEncoding]];
	[postBody appendData:[[NSString stringWithFormat:@"<yourcode/>"] dataUsingEncoding:NSUTF8StringEncoding]];
	[postBody appendData:[[NSString stringWithFormat:@"</xml>"] dataUsingEncoding:NSUTF8StringEncoding]];
	
	//post
	[request setHTTPBody:postBody];
	
	//get response
	NSHTTPURLResponse* urlResponse = nil;  
	NSError *error = [[NSError alloc] init];  
	NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];  
    return responseData;
}

+ (NSString*) uploadImage : (UIImage*) image;

{
    /*
     NSData *dataimage = [ConnectionHTTP doRequestgetData:(NSString *)@"ios/getImage.php"];
     imageView.image = [UIImage imageWithData:dataimage];
     */
    
    NSData *imageData = UIImageJPEGRepresentation(image, 10);
    // setting up the URL to post to
    NSString *urlString = @"http://192.168.1.105/ios/imagesupload.php";
    
    // setting up the request object now
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];
    
    
    NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
    
    
    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"imagename.jpg\"\r\n",index] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    // setting the body of the post to the reqeust
    [request setHTTPBody:body];
    
    // now lets make the connection to the web
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    //NSLog(@"image id log %@",returnString);
    
    return returnString;
}




@end
